package chuangya.com.anmi.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import SunStarUtils.GlideUtil;
import butterknife.ButterKnife;
import butterknife.InjectView;
import chuangya.com.anmi.R;
import chuangya.com.anmi.adapter.MainHotWordAdapter;
import chuangya.com.anmi.adapter.MainNewWordAdapter;
import chuangya.com.anmi.databean.HotWorkData;
import chuangya.com.anmi.util.LoadMoreFooterView;
import chuangya.com.anmi.util.PicassoLoader;
import chuangya.com.anmi.util.RefreshHeaderView;

/**
 * Created by Sun.Star on 2017/6/23.
 */

public class MainInnerFragment extends Fragment {
    @InjectView(R.id.main_inner_banner)
    Banner mainInnerBanner;
    @InjectView(R.id.main_inner_recommend_more)
    TextView mainInnerRecommendMore;
    @InjectView(R.id.main_inner_recommend1_img)
    ImageView mainInnerRecommend1Img;
    @InjectView(R.id.main_inner_recommend1_name)
    TextView mainInnerRecommend1Name;
    @InjectView(R.id.main_inner_recommend2_img)
    ImageView mainInnerRecommend2Img;
    @InjectView(R.id.main_inner_recommend2_name)
    TextView mainInnerRecommend2Name;
    @InjectView(R.id.main_inner_progress)
    ProgressBar mainInnerProgress;
    @InjectView(R.id.main_inner_hot_more)
    TextView mainInnerHotMore;
    @InjectView(R.id.main_inner_hot_rec)
    RecyclerView mainInnerHotRec;
    @InjectView(R.id.main_inner_new_more)
    TextView mainInnerNewMore;
    @InjectView(R.id.main_inner_new_rec)
    RecyclerView mainInnerNewRec;
    @InjectView(R.id.swipe_refresh_header)
    RefreshHeaderView swipeRefreshHeader;
    @InjectView(R.id.swipe_target)
    NestedScrollView swipeTarget;
    @InjectView(R.id.swipe_load_more_footer)
    LoadMoreFooterView swipeLoadMoreFooter;
    @InjectView(R.id.swip_layot)
    SwipeToLoadLayout swipLayot;
    private View thisView;
    private List<String> bannerList = new ArrayList<>();
    private List<String> bannerStringList = new ArrayList<>();
    private List<HotWorkData> hotWorkDataList = new ArrayList<>();
    private List<HotWorkData> newWorkDataList = new ArrayList<>();
    private MainHotWordAdapter adapter;
    private MainNewWordAdapter adapterLatest;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        thisView = inflater.inflate(R.layout.main_innerlayot, null);
        ButterKnife.inject(this, thisView);
        initBannerData();
        initHotData();
        initView();
        return thisView;
    }

    private void initHotData() {
        hotWorkDataList.clear();
        newWorkDataList.clear();
        for (int i = 0; i < 8; i++) {
            HotWorkData data = new HotWorkData();
            if (i == 1) {
                data.setRe(true);
                data.setLatest(true);
            } else if (i == 4) {
                data.setRe(true);
                data.setLatest(true);
            } else if (i == 6) {
                data.setRe(true);
                data.setLatest(true);
            }
            data.setTime("2016-01-01");
            data.setQi("306");
            data.setName("火影忍者");
            data.setImgUrl("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1458371161,243418480&fm=26&gp=0.jpg");
            hotWorkDataList.add(data);
            newWorkDataList.add(data);
        }
    }

    private void initBannerData() {
        bannerList.clear();
        bannerStringList.clear();
        bannerList.add("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1458371161,243418480&fm=26&gp=0.jpg");
        bannerList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498216730041&di=4018eced82c578d11648eb058719f10b&imgtype=0&src=http%3A%2F%2Fbizhi.zhuoku.com%2Fwall%2Fjie%2F20070409%2Fhuoying%2F092.jpg");
        bannerList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498216730036&di=f59502145f9201b0ddb0a17e60679e40&imgtype=0&src=http%3A%2F%2Fimg.youxiguancha.com%2Fgame%2F2017%2F01%2F11%2F1484100282_20.jpg");
        bannerList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1498216730035&di=7554cbfb070c09d0dbec65d9653a1a95&imgtype=0&src=http%3A%2F%2Fimgs.shougongke.com%2FPublic%2Fdata%2Fhand%2F201408%2F18%2Fstep%2F877945_1408373564074.jpg");
        bannerStringList.add("火影忍者1");
        bannerStringList.add("火影忍者2");
        bannerStringList.add("火影忍者3");
        bannerStringList.add("火影忍者4");
    }

    private void initView() {
        mainInnerBanner.setImageLoader(new PicassoLoader());
        mainInnerBanner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        mainInnerBanner.setBannerTitles(bannerStringList);
        mainInnerBanner.setImages(bannerList);
        mainInnerBanner.start();
        GlideUtil.ShowImage(getActivity(), "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1458371161,243418480&fm=26&gp=0.jpg", mainInnerRecommend1Img);
        GlideUtil.ShowImage(getActivity(), "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1458371161,243418480&fm=26&gp=0.jpg", mainInnerRecommend2Img);
        adapter = new MainHotWordAdapter(getActivity(), hotWorkDataList);
        mainInnerHotRec.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mainInnerHotRec.setAdapter(adapter);
        adapterLatest = new MainNewWordAdapter(getActivity(), newWorkDataList);
        mainInnerNewRec.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        mainInnerNewRec.setAdapter(adapterLatest);
        swipLayot.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipLayot.setRefreshing(false);
                    }
                },2000);
            }
        });
        swipLayot.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swipLayot.setLoadingMore(false);
                    }
                },2000);

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        swipLayot.setLoadingMore(false);
        swipLayot.setRefreshing(false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
